package com.longcai.cm.api.api4order;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.longcai.cm.bean.Page;
import com.longcai.cm.bean.R;
import com.longcai.cm.domain.JaGoodsCoupon;
import com.longcai.cm.service.JaGoodsCouponService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author libingxing
 */
@RestController
@Api(tags = "优惠券相关接口")
@RequestMapping("/api/coupon")
public class ApiCouponController {

    @Resource
    private JaGoodsCouponService goodsCouponService;

    @PostMapping("/goodsCouponList")
    @ApiOperation("团券列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "当前页", name = "page", dataType = "int", required = true),
            @ApiImplicitParam(value = "条数", name = "rows", dataType = "int", required = true),
    })
    public R<Page<List<JaGoodsCoupon>>> goodsCouponList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer rows
    ) {
        final LambdaQueryWrapper<JaGoodsCoupon> wrapper = new LambdaQueryWrapper<>();
        final long total = goodsCouponService.count(wrapper);
        final Page<List<JaGoodsCoupon>> result = new Page<>(page, rows, total);
        wrapper.last(result.convertPage());
        wrapper.orderByDesc(JaGoodsCoupon::getCreateTime);
        final List<JaGoodsCoupon> list = goodsCouponService.list(wrapper);
        result.setContent(list);
        return R.success(result);
    }

    @PostMapping("/goodsCouponInfo")
    @ApiOperation("团券详情")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "id", name = "id", dataType = "int", required = true)
    })
    public R<JaGoodsCoupon> goodsCouponInfo(
            String id
    ) {
        return R.success(goodsCouponService.getById(id));
    }

}
